package com.tang.login_session.controller;

import com.tang.login_session.pojo.User;
import com.tang.login_session.service.IUserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
public class SessionController {

    private final IUserService userService;

    public SessionController(IUserService userService) {
        this.userService = userService;
    }

    @PostMapping("/login")
    public String login(@RequestBody User user, HttpSession session) {
        if ("admin".equals(user.getUsername()) && "admin".equals(user.getPassword())) {
            session.setAttribute("user", user);
            return "登录成功";
        }
        return "账号或密码错误！";
    }

    @GetMapping("/logout")
    public String logout(HttpSession session) {
        session.removeAttribute("user");
        return "退出成功！";
    }

    @GetMapping("/api")
    public String api(HttpSession session) {
        userService.doSomething();
        return "成功返回数据！";
    }

    @GetMapping("/api2")
    public String api2(HttpSession session) {
        return "成功返回数据！";
    }

}
